package com.ohs.service.impl;

import com.ohs.mapper.UserMapper;
import com.ohs.pojo.User;
import com.ohs.service.IUserService;
import com.ohs.utils.JsonResult;
import com.ohs.utils.getMD5PwdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    //测试
    public User findOne() {
        return this.userMapper.findOne();
    }

    /**
     * 检查注册名
     * @param username
     * @return
     */
    public Map<String, Object> checkRegisterName(String username) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(username)){
            map.put("state", 201);
            map.put("msg", "注册名不能为空");
            return map;
        }
        User user = this.userMapper.findUserByUserName(username);
        if (user != null){
            map.put("state", 202);
            map.put("msg", "注册名已存在");
            return map;
        }
        map.put("state", 200);
        map.put("msg", "注册名可用");
        return map;
    }

    //注册
    public JsonResult doRegister(User user, String confirmPwd) {
        JsonResult jsonResult = new JsonResult();
        if (user == null){
            jsonResult.setState(201);
            jsonResult.setMessage("注册信息必填");
            return jsonResult;
        }
        if (StringUtils.isEmpty(user.getUserName())){
            jsonResult.setState(202);
            jsonResult.setMessage("注册名必填");
            return jsonResult;
        }
        if (StringUtils.isEmpty(user.getPassword())){
            jsonResult.setState(203);
            jsonResult.setMessage("注册密码必填");
            return jsonResult;
        }
        if (confirmPwd == null || confirmPwd == ""){
            jsonResult.setState(204);
            jsonResult.setMessage("确认密码不能为空");
            return jsonResult;
        }
        if (!confirmPwd.equals(user.getPassword())){
            jsonResult.setState(205);
            jsonResult.setMessage("两次密码不一致");
            return jsonResult;
        }
        User u = this.userMapper.findUserByUserName(user.getUserName());
        if (u != null){
            jsonResult.setState(206);
            jsonResult.setMessage("用户已存在");
            return jsonResult;
        }

        String salt = UUID.randomUUID().toString();
        String password = user.getPassword();

        String md5Pwd = getMD5PwdUtil.getMD5Pwd(password, salt);
        user.setPassword(md5Pwd);
        user.setSalt(salt);
        user.setModifyUser(user.getUserName());
        int row = this.userMapper.doRegister(user);
        if (row != 1){
            jsonResult.setState(207);
            jsonResult.setMessage("注册失败");
            return jsonResult;
        }

        jsonResult.setState(200);
        jsonResult.setMessage("注册成功");
        return jsonResult;
    }

    //检查登录名
    public JsonResult checkLoginName(String username) {
        JsonResult jsonResult = new JsonResult();
        if (username == null || username == ""){
            jsonResult.setState(201);
            jsonResult.setMessage("用户名不能为空");
            return jsonResult;
        }
        User user = this.userMapper.findUserByUserName(username);
        if (user == null){
            jsonResult.setState(202);
            jsonResult.setMessage("该用户不存在");
            return jsonResult;
        }

        jsonResult.setState(200);
        jsonResult.setMessage("用户名存在");
        return jsonResult;
    }

    //登录
    public JsonResult doLogin(String username, String password, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        if (username == null || username == ""){
            jsonResult.setState(201);
            jsonResult.setMessage("用户名不能为空");
            return jsonResult;
        }
        if (password == null || password == ""){
            jsonResult.setState(202);
            jsonResult.setMessage("密码不能为空");
            return jsonResult;
        }
        User user = this.userMapper.findUserByUserName(username);
        if (user == null){
            jsonResult.setState(203);
            jsonResult.setMessage("用户不存在");
            return jsonResult;
        }
        String salt = user.getSalt();
        String md5Pwd = getMD5PwdUtil.getMD5Pwd(password, salt);
        if (!md5Pwd.equals(user.getPassword())){
            jsonResult.setState(204);
            jsonResult.setMessage("用户名或密码不正确");
            return jsonResult;
        }

        session.setAttribute("uid", user.getUid());
        session.setAttribute("username", user.getUserName());
        session.setAttribute("avartar", user.getAvartar());

        jsonResult.setState(200);
        jsonResult.setMessage("登录成功");
        return jsonResult;
    }
}
